var drag = null;
var id = null;
var tid = null;
var cid = null;
var ftid = null;
var onResizedX = function(ftid,id){
	
	freeze();
	
	var table = $("#" + ftid);  
    var th = table.find(">thead>tr:first>th,>thead>tr:first>td"); 
	if(!th.length) th = table.find(">tbody>tr:first>th,>tr:first>th,>tbody>tr:first>td, >tr:first>td");
	th = th.filter(":visible");
	var colWidth = "";
	th.each(function(i){	
		if (i == id){
			var c = $(this);     
			colWidth += ',' + Math.max(c.width(),this.offsetWidth,this.clientWidth,this.scrollWidth)*72/96;//pxתpt
			return ;
		}
	});
	colWidth = colWidth.substring(1);
	// send width to instance obj
	var url = window.charts_server + "/Report-ResizeReport." + window.action_suffix; 
	var resultData = {};
    var array= $('#resultForm').serializeArray();;
    $(array).each(function(){
       resultData[this.name]=this.value;
    });
	resultData.width = colWidth;
	resultData.col = id + 1;
	resultData.tableName = ftid;

	var pro = ftid.substring(ftid.length - 1);
	if (pro == "2" || pro == "4"){
		var ftid1 = ftid.substring(0,ftid.length-1) + "1";
		var table = $("#" + ftid1);  
		var th = table.find(">thead>tr:first>th,>thead>tr:first>td"); 
		if(!th.length) th = table.find(">tbody>tr:first>th,>tr:first>th,>tbody>tr:first>td, >tr:first>td");
		th = th.filter(":visible");
		resultData.col = resultData.col + th.length;
	}
	
	$.ajax({   
	    type : "POST",   
	    url : url,
	    data : resultData,
	    dataType: "json",
	    success: function upload(value){
	    }
	});
};
var onResizedY = function(ftid,id){  
	
	freeze();
	
	var tr = getTr(ftid);	
	if(!tr) return;
	
	var rowHeight = "";
	tr.each(function(i){
		if (i == id){
			var c = $(this);     
			rowHeight += ',' + c.height()*72/96;
			return ;
		}
	});
	rowHeight = rowHeight.substring(1);
	// send width to instance obj
	var url = window.charts_server + "/Report-ResizeReport." + window.action_suffix; 
	var resultData = {};
    var array= $('#resultForm').serializeArray();;
    $(array).each(function(){
       resultData[this.name]=this.value;
    });
	resultData.height = rowHeight;
	resultData.row = id + 1;
	
	var pre = ftid.substring(0,ftid.length - 1);
	var pro = ftid.substring(ftid.length - 1);
	if (pro == "4" || pro == "3"){
		var tr1 = getTr(pre + "1");
		resultData.row += tr1.length;
	}
	
	resultData.tableName = ftid;
	
	$.ajax({   
	    type : "POST",   
	    url : url,
	    data : resultData,
	    dataType: "json",
	    success: function upload(value){
	    }
	});
};

var dragMouseOver = function (e){
	var e = e || window.event;
	if (!!drag) return;
	if (typeof e.target.parentNode == "undefined")return;
	var pt = e.target.parentNode;
	var j_pt = $(pt);
	var trId = null;
	var td = null;
	if ("TD" !== e.target.tagName && "TD" !== pt.tagName) {
		return;
	} else if ("TD" !== e.target.tagName){
		trId = pt.parentNode.id;
		cid = pt.cellIndex + pt.colSpan - 1;
		td = pt;
	} else {
		trId = pt.id;
		cid = e.target.cellIndex + e.target.colSpan - 1;
		td = e.target;
	}

	if(!!trId){
		var ids = trId.split("-");
//		if ("0" == ids[0])return;
		id = parseInt(ids[0]);
		tid = ids[1];
		ftid = ids[2];
		$("#rowresizer-"+tid).css({
			top: j_pt.offset().top - $("#" + tid).offset().top + j_pt.outerHeight(false) - 4 + "px"
		});
		$("#colresizer-"+tid).css({
			left: $(td).offset().left - $("#" + tid).offset().left + $(td).outerWidth(false) - 4 + "px"
		});
	}
}

var dragMouseMove = function (e){
	if (!drag) return;
	var e = e || window.event;
	if (drag.f === "Row"){
		var oy = e.pageY;
		var y =  oy - drag.oy + drag.t;
		var target = $(e.target);
		$("#rowresizerguide-" + tid).css({
			top: y + "px"
		});
		drag.y = y;
	} else {
		var target = $(e.target);
		var ox = e.pageX;
		var x =  ox - drag.ox + drag.l;
		var target = $(e.target);
		$("#colresizerguide-" + tid).css({
			left: x + "px"
		});
		drag.x = x;
	}
	
	return false;
}

var dragMouseUp = function (e){
	
	$(document).unbind('mouseup').unbind('mousemove');
	if (!drag) return;
	var e = e || window.event;
	if (drag.f === "Row"){
		$("#rowresizerguide-" + tid).css({		
			display: "none"
		});
		
		if (!(drag.y - drag.t == 0)) {
			syncRows(ftid,id);
			var i = ftid.substring(ftid.length - 1);
			if ("1" == i){
				var ftid2 = ftid.substring(0,ftid.length - 1) + "2";
				syncRows(ftid2,id);
			} else if ("2" == i){
				var ftid1 = ftid.substring(0,ftid.length - 1) + "1";
				syncRows(ftid1,id);
			} else if ("3" == i){
				var ftid4 = ftid.substring(0,ftid.length - 1) + "4";
				syncRows(ftid4,id);
			} else if ("4" == i){
				var ftid3 = ftid.substring(0,ftid.length - 1) + "3";
				syncRows(ftid3,id);
			}
			
			onResizedY(ftid,id);
		}
	} else {
		$("#colresizerguide-" + tid).css({		
			display: "none"
		});
		
		if (!(drag.x - drag.l == 0)) {
			syncCols(ftid,cid);
			var i = ftid.substring(ftid.length - 1);
			if ("1" == i){
				var ftid3 = ftid.substring(0,ftid.length - 1) + "3";
				syncCols(ftid3,cid);
			} else if ("2" == i){
				var ftid4 = ftid.substring(0,ftid.length - 1) + "4";
				syncCols(ftid4,cid);
			} else if ("3" == i){
				var ftid1 = ftid.substring(0,ftid.length - 1) + "1";
				syncCols(ftid1,cid);
			} else if ("4" == i){
				var ftid2 = ftid.substring(0,ftid.length - 1) + "2";
				syncCols(ftid2,cid);
			}
			
			onResizedX(ftid,cid);
		}
	}
	
	drag = null;
	
	return false;
}

var dragMouseDown = function (e){
	var e = e || window.event;
	var target = $(e.target);

	if("manualRowResizer" === target.attr("class")){
		drag = {};
		drag.f = "Row";
		drag.t = target.position().top;
		drag.y = drag.t;
		drag.oy = e.pageY;
		$("#rowresizerguide-" + tid).css({	
			top: target.offset().top - $("#" + tid).offset().top + target.outerHeight(false) - 4 + "px",
			display: "block"
		});
		$(document).bind('mousemove', dragMouseMove).bind('mouseup', dragMouseUp);
	} else if ("manualColumnResizer" === target.attr("class")) {
		drag = {};
		drag.f = "Col";
		drag.l = target.position().left;
		drag.x = drag.l;
		drag.ox = e.pageX;
		$("#colresizerguide-" + tid).css({	
			left: target.offset().left - $("#" + tid).offset().left + target.outerWidth(false) - 4 + "px",
			display: "block"
		});
		$(document).bind('mousemove', dragMouseMove).bind('mouseup', dragMouseUp);
	}
}

var syncRows = function(id,i){	
	var t = $("#" + id);
	if (typeof t == "undefined")return;
	var tr = t.find(">tbody>tr"); 
	if (!tr) return;
	if (typeof tr[0] == "undefined") return;
	if (tr[0].className.trim() == "") tr.splice(0,1);
   
	var inc = drag.y - drag.t, c = $(tr[i]); 			
	var h = c.height() + inc;
	c.height(h + "px");
	// 同步行标记
	$("#rowresizerguide-" + tid).css({	
		top: c.offset().top - $("#" + tid).offset().top + c.outerHeight(false) - 4 + "px",
	});
};

var getTr = function(ftid){
	var table = $("#" + ftid); 
	var tr = table.find(">tbody>tr"); //table trs are obtained
	if (!tr) return null;
	if (!tr[0]) return null;
	if (tr[0].className.trim() == "") tr.splice(0,1);
	
	return tr;
}

var syncCols = function(id, i){
	var t = $("#" + id);
	var th = t.find(">thead>tr:first>th,>thead>tr:first>td"); //table headers are obtained
	if(!th.length) th = t.find(">tbody>tr:first>th,>tr:first>th,>tbody>tr:first>td, >tr:first>td");	 //but headers can also be included in different ways
	th = th.filter(":visible");
	var cg = t.find("col");

	var length = th.length;
	if (i > length - 1){
		return;
	}
	var inc = drag.x - drag.l; 
	var c = $(th[i]); 			
	var w = Math.max(c.width(),th[i].offsetWidth,th[i].clientWidth,th[i].scrollWidth) + inc;	
	var tw = t.width() + inc + 1;
	
	t.css('width', tw);
//	c.css('width', w);
	c.width(w + "px");		
	cg.eq(i).width(w + "px"); 
    t.css('min-width',tw);
    
	if(w < 16){
		tw = tw + 16 - w;
		w = 16;
		t.css('width', tw);
//		c.css('width', w);
		c.width(w + "px");		
		cg.eq(i).width(w + "px"); 
	    t.css('min-width',tw);
		$("#colresizerguide-" + tid).css({	
			left: c.offset().left - $("#" + tid).offset().left + c.outerWidth(false) - 4 + "px",
		});
	}

    var idArr = id.split("_");
    
    if (idArr.length == 3 && (idArr[2] == "table1" || idArr[2] == "table3")){
    	$("#" + idArr[0] + "_" + idArr[1] + "_div1").width(tw);
    	if (typeof $("#" + idArr[0] + "_" + idArr[1] + "_div3") != "undefined"){
    		$("#" + idArr[0] + "_" + idArr[1] + "_div3").width(tw);
    	}
    }
    
    return false;
};